using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharePoint.ImportExport.Core;
namespace CodePlex.ExtractedDataReorganizer
{
    public partial class frmEditListTemplate : Form
    {
        public SharePoint.ImportExport.Core.ContentType template = null;
        public bool TemplateChanged = false;
        public frmEditListTemplate(SharePoint.ImportExport.Core.ContentType Template)
        {
            template = Template;
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            template.Name = txtName.Text;
            template.BaseType = txtBaseTemplate.Text;

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void frmEditListTemplate_Load(object sender, EventArgs e)
        {
            txtName.Text = template.Name;
            txtBaseTemplate.Text=template.BaseType;
            foreach (SharePoint.ImportExport.Core.Field field in template.Fields)
            {
                fieldsList.Items.Add(field.DisplayName);
            }

        }

        private void txtDescription_TextChanged(object sender, EventArgs e)
        {

        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void editFieldsContextMenu_Opening(object sender, CancelEventArgs e)
        {

        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ArrayList al = new ArrayList(fieldsList.CheckedIndices.Count);
            foreach ( int idx in fieldsList.CheckedIndices)
            {
                al.Add(fieldsList.Items[idx]);
                
            }
            SharePoint.ImportExport.Core.FieldsCollection fc = new FieldsCollection();
            foreach (SharePoint.ImportExport.Core.Field f in template.Fields)
            {
                bool found = false;
                foreach (object o in al)
                {
                    if((string)o==f.DisplayName)
                    {
                        found=true;
                        break;
                    }

                }
                if (!found)
                    fc.Add(f);
                else
                {
                    //Cleanup the item as it is already removed
                    int idxtoremove=-1;
                    for (int i = 0; i <= al.Count; i++)
                    {
                        if((string)al[i]==f.DisplayName)
                        {

                            idxtoremove=i;
                            break;
                        }
                    }
                    if (idxtoremove != -1)
                        al.RemoveAt(idxtoremove);

                }
            }
            SharePoint.ImportExport.Core.ContentType contentType = new ContentType();
            contentType.BaseType=template.BaseType;
            contentType.Description=template.Description;
            contentType.EnableAttachments = template.EnableAttachments;
            contentType.EnableVersioning = template.EnableVersioning;
            contentType.Name = template.Name;
            contentType.NameAttribute = template.NameAttribute;
            contentType.PropertyBag = template.PropertyBag;
            contentType.Title = template.Title;
            foreach (SharePoint.ImportExport.Core.View vi in template.Views)
                contentType.Views.Add(vi);

            foreach (SharePoint.ImportExport.Core.Field fi in fc)
                contentType.Fields.Add(fi);
            
            this.template = contentType;
            TemplateChanged = true;
            

        }


    }
}